//package medicalApp.velociraptors.com;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.AbstractButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

/**
 * GUI panel for the SysAdmin User
 * 
 * @author Lsun
 */

@SuppressWarnings("serial")
public class SysAdminPanel extends JPanel {
	protected Integer chosenSSN;
	private int SSN;
	public JTable tblDoctorPatientList;
	private JScrollPane scrollPane;
	
	private MedAppController mac;
	private JTextPane lblSysAdminInfo;
	private JTextPane lblSysAdminInfo2;

//	/**
//	 * Launch the application.
//	 */
//	public static void main(String[] args) {
//		try {
//			SysAdminPanel dialog = new SysAdminPanel(new MedAppController(new MedAppGUI()));
//			dialog.setVisible(true);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
	
	/**
	 * Set the SysAdmin text
	 * 
	 * @param firstName
	 * @param lastName
	 * @param currentUser
	 * @param SSN
	 */
	public void setInfo(String firstName, String lastName, String currentUser, int SSN) {
		mac.out.println("Label: "+ lblSysAdminInfo2.getText());
		lblSysAdminInfo2.setText("System Admin " + firstName+" "+lastName+"\n"+ "Username: " + currentUser);
			this.SSN = SSN;	
	}
	
	/**
	 * Create the panel with parameter Controller
	 * 
	 * @param mac the MedAppController that drives this panel.
	 */
	public SysAdminPanel(final MedAppController mac) {
		this.mac = mac;
		setBackground(UIManager.getColor("Button.background"));
		setPreferredSize(new Dimension(800,600));
		setLayout(null);
		
		JSeparator separator = new JSeparator();
		separator.setForeground(Color.GRAY);
		separator.setBounds(0, 100, 150, 2);
		add(separator);
		
		JSeparator separator_1 = new JSeparator();
		separator_1.setOrientation(SwingConstants.VERTICAL);
		separator_1.setBounds(150, 0, 2, 600);
		add(separator_1);
		
		JButton button = new JButton("Add");
		button.addMouseListener(new MouseAdapter() {
			/**
			 * Designed to perform the action specified upon triggering.
			 * 
			 * @param arg0 The ActionEvent that triggers the method call.
			 */ 
			@Override
			public void mouseClicked(MouseEvent arg0) {
				AddUserDialog adp = new AddUserDialog(mac);
				adp.setVisible(true);
			}
		});
		button.setBounds(33, 182, 89, 23);
		add(button);
		
		JButton button_1 = new JButton("Delete");
		button_1.addMouseListener(new MouseAdapter() {
			/**
			 * Designed to perform the action specified upon triggering.
			 * 
			 * @param arg0 The ActionEvent that triggers the method call.
			 */ 
			@Override
			public void mouseClicked(MouseEvent arg0) {
				mac.out.println("chosenSSN: "+chosenSSN);
				if(chosenSSN!=null){
					int n = JOptionPane.showConfirmDialog(null, "Do you want to delete this patient?");
					if(n == JOptionPane.YES_OPTION){

						try {
							String sql = "DELETE FROM `user` WHERE U_SSN = '"+chosenSSN+"'";
							mac.out.println(sql);
							Connection con = mac.getCon();
							Statement stmt = con.createStatement();
							int rs = stmt.executeUpdate(sql);
							JOptionPane.showMessageDialog(null, "User Deleted!");
							mac.setSysAdminPatientTableModel();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				else
					JOptionPane.showMessageDialog(null, "Select a User!");
			}
		});
		button_1.setBounds(33, 235, 89, 23);
		add(button_1);
		
		JButton button_2 = new JButton("Lookup");
		button_2.addActionListener(new ActionListener() {
			/**
			 * Designed to perform the action specified upon triggering.
			 * 
			 * @param arg0 The ActionEvent that triggers the method call.
			 */ 
			public void actionPerformed(ActionEvent arg0) {
				LookupUser lp = new LookupUser(mac, SSN);
				lp.setVisible(true);
			}
		});
		button_2.setBounds(33, 289, 89, 23);
		add(button_2);
		
		JLabel label_1 = new JLabel("Actions");
		label_1.setBounds(10, 157, 112, 14);
		add(label_1);
		
		JSeparator separator_2 = new JSeparator();
		separator_2.setForeground(Color.GRAY);
		separator_2.setBounds(0, 400, 150, 2);
		add(separator_2);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(188, 21, 589, 522);
		add(scrollPane);
		
		JLabel lblNewLabel = new JLabel(new ImageIcon("135Hospital-Administration-Velociraptors-Attack.jpg"));
		lblNewLabel.setBounds(10, 413, 130, 130);
		add(lblNewLabel);

		JButton btnLogout = new JButton("Logout");
		btnLogout.addActionListener(new ActionListener() {
			/**
			 * Designed to perform the action specified upon triggering.
			 * 
			 * @param arg0 The ActionEvent that triggers the method call.
			 */ 
			public void actionPerformed(ActionEvent arg0) {
				mac.setCurrentUser("", "");
				mac.setCurrentCard("Login");
				mac.setLoginSuccess(false);
//				mac.reset();
			}
		});
		btnLogout.setBounds(33, 554, 89, 23);
		add(btnLogout);
		
		lblSysAdminInfo2 = new JTextPane();
		lblSysAdminInfo2.setBackground(UIManager.getColor("Button.background"));
		lblSysAdminInfo2.setBounds(10, 11, 130, 78);
		add(lblSysAdminInfo2);
	}
	
	/**
	 * The tableInit method initializes the table for use in the database.
	 */ 
	public void tableInit(){
		try {
			tblDoctorPatientList = new JTable(myModel());

			tblDoctorPatientList.setFillsViewportHeight(true);
			scrollPane.setViewportView(tblDoctorPatientList);

			tblDoctorPatientList.addMouseListener(new MouseAdapter() {
				/**
				 * Designed to perform the action specified upon triggering by mouse input.
				 * 
				 * @param arg0 The MouseEvent that triggers the method call.
				 */ 
				@Override
				public void mouseClicked(MouseEvent arg0) {
					if (arg0.getClickCount() == 2) {
						JTable target = (JTable)arg0.getSource();
						int row = target.getSelectedRow();
						try{
							chosenSSN = (Integer) target.getValueAt(row, 2);
							UserView dpv = new UserView(mac, chosenSSN, SSN);
							dpv.setVisible(true);
						}catch(Exception e){}

					}
					if (arg0.getClickCount() == 1) {
						JTable target = (JTable)arg0.getSource();
						int row = target.getSelectedRow();
						try{
							chosenSSN = (Integer) target.getValueAt(row, 2);
						}catch(Exception e){}
					}
				}
			});
		} catch (Exception e) {
			// TODO Auto-generated catch block
						e.printStackTrace();
		}
	}
	
	/**
	 * The myModel method establishes the TableModel that powers the class.
	 */ 
	public TableModelAdmin myModel() throws Exception{
		TableModelAdmin model = new TableModelAdmin(mac);
		return model;
	}

	/**
	 * Getter for tblDoctorPatientList
	 * @return the tblDoctorPatientList
	 */
	public JTable getTblDoctorPatientList() {
		return tblDoctorPatientList;
	}

	/**
	 * Setter for tblDoctorPatientList
	 * @param tblDoctorPatientList the tblDoctorPatientList to set
	 */
	public void setTblDoctorPatientList(JTable tblDoctorPatientList) {
		this.tblDoctorPatientList = tblDoctorPatientList;
	}

	/**
	 * Getter for chosenSSN
	 * @return the chosenSSN
	 */
	public Integer getChosenSSN() {
		return chosenSSN;
	}

	/**
	 * Setter for chosenSSN
	 * @param chosenSSN the chosenSSN to set
	 */
	public void setChosenSSN(Integer chosenSSN) {
		this.chosenSSN = chosenSSN;
	}

	/**
	 * Getter for drSSN
	 * @return the drSSN
	 */
	public int getDrSSN() {
		return SSN;
	}

	/**
	 * Setter for drSSN
	 * @param drSSN the drSSN to set
	 */
	public void setDrSSN(int drSSN) {
		this.SSN = drSSN;
	}
}
